package com.lanou.beautifulwardrobe.home.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.IntroElement;

import java.util.List;

/**
 * Created by dllo on 15/11/6.
 */
public class IntroFRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<IntroElement> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private ImageLoader imageLoader;

    public void addData(List<IntroElement> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case TYPE0:
                View titleView = LayoutInflater.from(parent.getContext()).inflate(R.layout.intro_title_item, null);
                holder = new TitleViewHolder(titleView);
                break;
            case TYPE1:
                View bodyView = LayoutInflater.from(parent.getContext()).inflate(R.layout.intro_body_item, null);
                holder = new BodyViewHolder(bodyView);
                break;
            case TYPE2:
                View imageView = LayoutInflater.from(parent.getContext()).inflate(R.layout.intro_image_item, null);
                holder = new ImageViewHolder(imageView);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (datas != null && datas.size() > 0) {
            switch (getItemViewType(position)) {
                case TYPE0:
                    TitleViewHolder titleHolder = (TitleViewHolder) holder;
                    titleHolder.introTitle.setText(datas.get(position).getContent());
                    break;
                case TYPE1:
                    BodyViewHolder bodyHolder = (BodyViewHolder) holder;
                    bodyHolder.introBody.setText(datas.get(position).getContent());
                    break;
                case TYPE2:
                    final ImageViewHolder imageHolder = (ImageViewHolder) holder;

                    getBitMap(datas.get(position).getContent(), imageHolder.introImage);

                    break;

            }
        }
    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    public static final int TYPE0 = 0;
    public static final int TYPE1 = 1;
    public static final int TYPE2 = 2;

    @Override
    public int getItemViewType(int position) {
        if (datas != null && datas.size() > 0) {
            String type = datas.get(position).getElement_type();
            if (type.equals("title")) {
                return TYPE0;
            } else if (type.equals("body")) {
                return TYPE1;
            } else if (type.equals("image")) {
                return TYPE2;
            }
        }
        return 0;
    }

    class TitleViewHolder extends RecyclerView.ViewHolder {
        private TextView introTitle;

        public TitleViewHolder(View itemView) {
            super(itemView);
            introTitle = (TextView) itemView.findViewById(R.id.introTitle);
        }
    }

    class BodyViewHolder extends RecyclerView.ViewHolder {
        private TextView introBody;

        public BodyViewHolder(View itemView) {
            super(itemView);
            introBody = (TextView) itemView.findViewById(R.id.introBody);
        }
    }

    class ImageViewHolder extends RecyclerView.ViewHolder {
        private NetworkImageView introImage;

        public ImageViewHolder(View itemView) {
            super(itemView);
            introImage = (NetworkImageView) itemView.findViewById(R.id.introImage);
        }
    }
    /** 拉取图片 **/
    private void getBitMap(String url, NetworkImageView imageView){
        imageLoader = volleySingleton.getImageLoader();
        imageView.setDefaultImageResId(R.mipmap.ic_launcher);
        imageView.setErrorImageResId(R.mipmap.ic_launcher);
        imageView.setImageUrl(url, imageLoader);
    }

}
